package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 刘昱江
 * 时间 2021/5/11
 */

@RestController
@CrossOrigin
@RequestMapping("/user")/*抽取公共请求路径*/
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/hello")
    public List<User> hello(){

        return userService.findAll();
    }

    /*
    * 业务需求:用户登录校验
    * URL: /user/login
    * 参数:username/password,json串
    * 返回值:SysResult对象,data的string类型的信息 token
    * */

    /**
     *
     * @param user
     * @return
     */
    @PostMapping("/login")
    public SysResult login(@RequestBody User user){
        System.out.println(user);
        //1.根据用户名和密码校验,返回token, service层中完成
        String token = userService.login(user);
        //2.token有值业务正确,如果为null,表示业务操作失败
        if(token == null){
            return SysResult.fail();
        }
        return SysResult.success(token);
    }

    @GetMapping("/list")
    public SysResult getUserList(PageResult pageResult){
        pageResult = userService.getUserList(pageResult);
        return SysResult.success(pageResult);
    }

    /*(`/user/status/${id}/${status}`*/
    @PutMapping("/status/{id}/{status}")
    public SysResult updateStatus(User user){
        userService.updateStatus(user);
        return SysResult.success();
    }
    @PostMapping("/addUser")
    public SysResult addUser(@RequestBody User user){
        userService.addUser(user);
        return SysResult.success();
    }

    @PutMapping("/updateUser")
    public SysResult updateUser(@RequestBody User user){
        userService.updateUser(user);
        return SysResult.success();
    }

    @GetMapping("/{id}")
    public SysResult getById(User user){
        User userById = userService.getById(user);
        return SysResult.success(userById);
    }
    @DeleteMapping("/{id}")
    public SysResult deleteUser(@PathVariable Integer id){
       userService.deleteUser(id);
       return SysResult.success();
    }

}
